<?php


/**
 * 格式化size显示 PHP版本过老会报错
 * @param float $size
 * @param int $times
 * @return string
 */
function formatSize(float $size, int $times = 0): string
{
    if ($size > 1024) {
        $size /= 1024;
        return formatSize($size, $times + 1); // 递归处理
    } else {
        switch ($times) {
            case '0':
                $unit = ($size == 1) ? 'Byte' : 'Bytes';
                break;
            case '1':
                $unit = 'KB';
                break;
            case '2':
                $unit = 'MB';
                break;
            case '3':
                $unit = 'GB';
                break;
            case '4':
                $unit = 'TB';
                break;
            case '5':
                $unit = 'PB';
                break;
            case '6':
                $unit = 'EB';
                break;
            case '7':
                $unit = 'ZB';
                break;
            default:
                $unit = '单位未知';
        }
        return sprintf('%.2f', $size) . $unit;
    }
}

function GetList(string $Shorturl, string $Dir, bool $IsRoot, string $Password)
{
    $Url = 'https://pan.baidu.com/share/wxlist?channel=weixin&version=2.2.2&clienttype=25&web=1';

    $Root = ($IsRoot) ? "1" : "0";
    $Dir = urlencode($Dir);
    $Data = "shorturl=$Shorturl&dir=$Dir&root=$Root&pwd=$Password&page=1&num=1000&order=time";
    $header = array("User-Agent: netdisk", "Referer: https://pan.baidu.com/disk/home");
    $result = json_decode(post($Url, $Data, $header), true);
    if (DEBUG) {
        echo '<pre>GetList():';
        var_dump($result);
        echo '</pre>';
    }
    return $result;
}

/**
 * 用于获取账号状态
 *
 * @return array [errno,会员状态,用户名,登录状态,会员剩余时间]
 */
function AccountStatus(string $BDUSS, string $STOKEN)
{
    $Url = "https://pan.baidu.com/api/gettemplatevariable?channel=chunlei&web=1&app_id=250528&clienttype=0";
    $Header = [
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.514.1919.810 Safari/537.36",
        "Cookie: BDUSS=$BDUSS;STOKEN=$STOKEN;"
    ];
    $Data = "fields=[%22username%22,%22loginstate%22,%22is_vip%22,%22is_svip%22,%22is_evip%22]";
    $Result = post($Url, $Data, $Header);
    $Result = json_decode($Result, true);
    if (DEBUG) {
        echo '<pre>账号状态:';
        var_dump($Result);
        echo '</pre>';
    }
    if ($Result["errno"] == 0) {
        // 正常
        $Username = $Result["result"]["username"];
        $LoginStatus = $Result["result"]["loginstate"];
        if ($Result["result"]["is_vip"] == 1) {
            $SVIP = 1; //会员账号
        } elseif ($Result["result"]["is_svip"] == 1 or $Result["result"]["is_evip"] == 1) {
            $SVIP = 2; //超级会员账号
        } else {
            $SVIP = 0; //普通账号
            return array(0, $SVIP, $Username, $LoginStatus, 0);
        }

        $Url = "https://pan.baidu.com/rest/2.0/membership/user?channel=chunlei&web=1&app_id=250528&clienttype=0";
        $Header = [
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.514.1919.810 Safari/537.36",
            "Cookie: BDUSS=$BDUSS;STOKEN=$STOKEN;"
        ];
        $Data = "method=query";
        $Result = post($Url, $Data, $Header);
        $Result = json_decode($Result, true);
        if (DEBUG) {
            echo '<pre>会员状态:';
            var_dump($Result);
            echo '</pre>';
        }
        if (isset($Result["reminder"]["svip"])) {
            //存在会员信息
            $LeftSeconds = $Result["reminder"]["svip"]["leftseconds"];
            return array(0, $SVIP, $Username, $LoginStatus, $LeftSeconds);
        }
    } elseif ($Result["errno"] == -6) {
        // 账号失效
        return array(-6);
    } else {
        //未知错误
        return array($Result["errno"]);
    }
}
/**
 * 时间差计算
 *
 * @param Timestamp $time
 * @return String Time Elapsed
 * @author Shelley Shyan
 * @copyright http://phparch.cn (Professional PHP Architecture)
 */
function time2Units(int $time)
{
    $year   = floor($time / 60 / 60 / 24 / 365);
    $time  -= $year * 60 * 60 * 24 * 365;
    $month  = floor($time / 60 / 60 / 24 / 30);
    $time  -= $month * 60 * 60 * 24 * 30;
    $day    = floor($time / 60 / 60 / 24);
    $time  -= $day * 60 * 60 * 24;
    $hour   = floor($time / 60 / 60);
    $time  -= $hour * 60 * 60;
    $minute = floor($time / 60);
    $time  -= $minute * 60;
    $second = $time;
    $elapse = '';

    $unitArr = array(
        '年'  => 'year', '个月' => 'month',  '天' => 'day',
        '小时' => 'hour', '分钟' => 'minute', '秒' => 'second'
    );

    foreach ($unitArr as $cn => $u) {
        if ($$u > 0) {
            $elapse = $$u . $cn;
            break;
        }
    }

    return $elapse;
}
